import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestSocket {
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    public static void main(String[] args) {
        SocketChannel socketChannel = null;
        try {
            System.out.println(getTime() + "\tstart connect");
            String host = args[0];
            int  port = Integer.parseInt(args[1]);
            Socket socket=new Socket(host,port);
            OutputStream outputStream=socket.getOutputStream();
            PrintWriter printWriter=new PrintWriter(outputStream);
            printWriter.write("hello_world");
            printWriter.flush();
            printWriter.close();
            outputStream.close();
            socket.close();

            System.out.println(getTime() + "\tconnect success");
        } catch (IOException e) {
            System.out.println(getTime() + "\tconnect exception");
            e.printStackTrace();
        }
    }

    private static String getTime() {
        return format.format(new Date());
    }
}
